//
//  SQGoodsView.m
//  pai
//
//  Created by pro on 14-11-20.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQGoodsView.h"
#import "Global.h"

@implementation SQGoodsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        
    }
    return self;
}
+(SQGoodsView*)initmyview
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"SQGoodsView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
-(void)setDic:(NSDictionary *)dic
{
    
}
-(void)setItem:(product_sku_t)item
{
    self.goodsPrice = item.price;
    self.canUseMaxPoints = item.point;
    
    self.mypoint = [Global instance].userinfo.points ;
    self.myprice = [Global instance].userinfo.money;
    
    [self.nameLabel setText:[NSString stringWithUTF8String:item.sku_name]];
    [self.priceLabel setText:[NSString stringWithFormat:@"%d",self.goodsPrice]];
    [self.pointLabel setText:@"0"];
    [self.pointMsgLabel setText:[NSString stringWithFormat:@"分(最多可使用%d积分)",self.canUseMaxPoints]];
    self.pointLabel.delegate = self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [super touchesBegan:touches withEvent:event];
}
- (IBAction)doActionClickBtn:(id)sender {
    
    UIButton* btn =(UIButton*)sender;
    NSString* str = btn.titleLabel.text;
    NSLog(@"str:%@",str);
    NSInteger num = [[str substringToIndex:str.length-1] integerValue];
    NSLog(@"num:%d",num);
    NSLog(@"num:%d",self.canUseMaxPoints);
    if (self.canUseMaxPoints < num) {
        num = self.canUseMaxPoints;
    }
    if (self.mypoint <num) {
        num = self.mypoint;
    }
    self.pointLabel.text = [NSString stringWithFormat:@"%d", num];
    self.priceLabel.text = [NSString stringWithFormat:@"%d", self.goodsPrice - num];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger num = [textField.text intValue];
    if (self.canUseMaxPoints < num) {
        num = self.canUseMaxPoints;
    }
    if (self.mypoint <num) {
        num = self.mypoint;
    }
    self.pointLabel.text = [NSString stringWithFormat:@"%d", num];
    self.priceLabel.text = [NSString stringWithFormat:@"%d", self.goodsPrice - num];
}
@end
